package com.omesoft.hksnore.util.snore;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.omesoft.hksnore.HMInfo;
import com.omesoft.hksnore.util.constant.CusConstant;
import com.omesoft.hksnore.util.data.MySharedPreferencesUtil;
import com.omesoft.hksnore.util.file.CreateFiles;
import com.omesoft.hksnore.util.photo.SettingUtil;

import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Omesoft on 2017/3/5.
 * <p>
 * 鼾声监测，实现runnable接口
 */

public class SnoreMonitor extends Thread {
    // 常量参数
    private int VSnoren = 40;// 鼾声分贝大小
    private int TSnoreMin = 600;// 鼾声时长最小值,毫秒数
    private int TSnoreMax = 3000;// 鼾声时长最大值,毫秒数
    // private int TNoSnoreMin = 1400;// 鼾声时长最小值,毫秒数
    // private int TNoSnoreMax = 4000;// 鼾声时长最大值,毫秒数
    private int TSnoreRateMin = 2800;// 鼾声时长最小值,毫秒数
    private int TSnoreRateMax = 5500;// 鼾声时长最大值,毫秒数
    private int TTureMax = 4000;// 鼾声时长最大值,毫秒数
    private float SnoreCoefficient = 5.0f;// 差值系数
    // private int SnorePoint = 3;// 连续大于阀值的点
    private int SnoreCountCon = 5;// 连续N次鼾声
    // 变量参数
    private long snoreStartTime = 0;// 鼾声开始时刻
    private long snoreStopTime = 0;// 鼾声结束时刻
    private long lastSnoreStartTime = 0;// 上一次鼾声开始时刻
    private long lastSnoreStopTime = 0;// 上一次鼾声结束时刻
    private long lastSnoreTime = 0;// 上一次鼾声开始时刻
    // private long snoreTime = 0;// 下次鼾声开始时刻
    private long noSnoreTime = 0;// 下次鼾声开始时刻
    private List<Long> snoreCountForTempTime = new ArrayList<Long>();
    ;// 最近鼾声时间

    private long Hour = 3600000;

    // private int dbSumFor2min = 0;
    private int snoreCountForTemp = 0;// 鼾声次数缓存累计
    private int snoreCountAtNight = 0;// 鼾声次数累计
    private float waveCrestValue = 0;
    private long waveCrestTime = 0;
    private long lastWaveCrestTime = 0;
    private float lastWaveCrestValue = 0;
    // private long newActTime = 0;
    private long lastActTime = 0;
    private long snoreLastActTureTime = 0;
    private int snore2AllActCount = 0;
    private int actCountFor2min = 0;
    private int snore2AllTurnCount = 0;
    // double simpleMax = 0, simpleMin = 100;
    private int sampleCount = 0;
    private int samplePreSec = 5;
    private long waitTime = 0;
    private float lastCompareTmp = 0;
    private float lastDB = 0;
    private float waveSimpleAvg = 40;
    private float minSnoreSimpleAvg = 40;
    private List<Float> snoreDBTmpList = new ArrayList<Float>();

    private List<Long> snoreCurTimeTmpList = new ArrayList<Long>();
    // private long recodeStartTime = 0;
    private boolean isRecodeStartTime = false;
    private long snoreAccountTime = 0;// 鼾声开始时刻
    private List<Float> compareTmpList = new ArrayList<Float>();
    private float lastDbavMaxTmp = 0;
    private float breathRate = 16.0f;
    // ----
    private List<Float> showedList0 = new ArrayList<Float>();
    private List<Float> showedList01 = new ArrayList<Float>();
    private List<Float> showedList1 = new ArrayList<Float>();
    private List<Float> showedList11 = new ArrayList<Float>();
    private long drawTime = 0;// 鼾声开始时刻
    private int maxNum = 0;
    private int snore10AllTurnCount = 0;// 10分钟翻身次数
    private List<HMInfo> allTurnCount = new ArrayList<HMInfo>();// 存放10分钟翻身次数的列表
    private long turnCountStartTime = 0;// 10分钟翻身开始的时间
    private int intervalTime = 300000;// 翻身次数间隔取样时长

    private int snore5AllMoveCount = 0;//
    private List<HMInfo> allMove = new ArrayList<HMInfo>();// 存放10分钟体动次数的列表
    private long moveStartTime = 0;// 10分钟体动开始的时间
    private int moveTime = 300000;// 体动次数间隔取样时长
    private long lastVBTime = 0;// 上一次振动的时间
    private int vbLv = 0;// 振动叠加等级

    private int vbLvCount = 0;// 止鼾次数
    // ---------------------160127
    private float compareTmpAvg = 18.0f;// 低点平均值
    // --------------------------------首页不同变量

    private int sleepTime = 200;// 线程等待时间
    private boolean isLoop = true;
    private Context context;
    private Handler handler;
    private SnoreRecord recorder;
    private short[] buffer;
    private int bufferSize;

    private long startTime = 0;
    private long syncTime = 0;
    private long syncDetialTime = 0;

    // 存储读取到的数据
    private FileOutputStream fos;

    /* 是否插入到数据库*/
    private boolean isInserSnore = false;

    // fft
    // private int length = 256;
    // private ArrayList<short[]> inBuf = new ArrayList<short[]>();// 原始录入数据
    // private ArrayList<int[]> outBuf = new ArrayList<int[]>();// 处理后的数据

    private String fileName;
    private String sleepID;

    public SnoreMonitor(Context context, Handler handler, String sleepID) {
        this.context = context;
        this.handler = handler;
        this.sleepID = sleepID;
        bufferSize = SnoreRecord.GetBufferSize();
        buffer = new short[bufferSize];

    }

    @Override
    public void run() {

        fileName = CreateFiles.CreateText(sleepID);
        Log.i("filenameTemp", "filenameTemp    " + fileName);
        isLoop = true;
        recorder = SnoreRecord.Create();
        recorder.start();

        lastSnoreTime = System.currentTimeMillis();
        waitTime = System.currentTimeMillis();
        startTime = System.currentTimeMillis();// 记录开始的时间
        drawTime = System.currentTimeMillis();
        syncTime = System.currentTimeMillis();
        syncDetialTime = System.currentTimeMillis();
        // recodeStartTime = System.currentTimeMillis();
        while (isLoop) {
            try {
                if (recorder != null) {
                    // 发送采样频率
                    sampleCount++;
                    if (System.currentTimeMillis() - waitTime >= 1000) {
                        waitTime = System.currentTimeMillis();
                        if (sampleCount > 1) {
                            samplePreSec = sampleCount;
                            // sendMsg(4, sampleCount);
                        }
                        sampleCount = 0;
                    }

                    int res = recorder.read(buffer, 0, bufferSize);// 读取数据流
                    Double curdB = DecibelUtil.getDecibel(buffer, res);// 转换为响度，dB
                    long curTime = System.currentTimeMillis();

                    if (System.currentTimeMillis() - syncDetialTime >= 120000) {//2分钟发送同步子表的指令
                        sendMsg2(CusConstant.SYNC_DETIAL, "");
                        syncDetialTime = System.currentTimeMillis();

                    }

                    if (System.currentTimeMillis() - syncTime >= 1800000) {//30分钟发送同步主表指令
                        sendMsg2(CusConstant.SYNC_ALL, "");
                        syncTime = System.currentTimeMillis();

                    }
                    // 计算当前时间与开始的时间差，即当前的监测时长
                    String nowTestTime = SettingUtil.getTimeDifference(context,
                            startTime, curTime);
                    sendMsg2(CusConstant.TEST_TIME, nowTestTime);
                    // if (false)
                    try {
                        if (curdB > 0 && curdB < 120) {
                            // 对DB的值保留两位小数
                            DecimalFormat d = new DecimalFormat("#.00");
                            float dB = Float.parseFloat(d.format(curdB));
                            // 保存DB值到TXT
                            if (fileName.length() > 0) {
                                CreateFiles.print(dB, fileName);
                            }
                            avgData(curTime, dB);// 处理画线和计算数据

                            if (System.currentTimeMillis() - drawTime > 200) {
                                drawTime = System.currentTimeMillis();
                                sendMsg2(0, showedList0);
                                sendMsg2(1, showedList01);
                                sendMsg2(CusConstant.DRAW_LINE, showedList1);
                                sendMsg2(18, showedList11);

                            }

                        } else {
                            Log.i("sendMsg2", "curdB    " + curdB);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                } else {
                    recorder = SnoreRecord.Create();
                    recorder.start();
                    buffer = new short[bufferSize];
                }
                // 休眠控制采样
                sleep(sleepTime);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (recorder != null) {
            recorder.clean();
            recorder = null;
        }
        try {
            if (fos != null) {
                fos.close();
                fos = null;
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 停止录音
     */
    private void stopRecord() {
        recorder.closeWriteMP3();
    }

    /**
     * 处理平滑曲线
     *
     * @param curTime
     * @param
     */
    private void avgData(long curTime, float dB) {
        // 加入缓存LIST
        snoreDBTmpList.add(dB);
        snoreCurTimeTmpList.add(curTime);
        // 平滑点数，以每秒采样来处理
        // samplePreSec = 6;
        if (snoreDBTmpList.size() > samplePreSec) {
            float dbAvg = 0;
            float dbSum = 0;
            float dbMax = 0;// 160128
            float calcPs = samplePreSec * 1.0f;
            // Log.d("sherlock", "calcPs::" + calcPs);
            for (int j = 0; j < snoreDBTmpList.size(); j++) {
                dbSum += snoreDBTmpList.get(j);
                dbMax = Math.max(dbMax, snoreDBTmpList.get(j));// 160128
            }

            dbAvg = dbSum / snoreDBTmpList.size() * 0.8f + dbMax * 0.2f;// 160128
            dbAvg = Math.round(dbAvg * 10) / 10.0f;
            minSnoreSimpleAvg = Math.min(minSnoreSimpleAvg, dbAvg)
                    + SnoreCoefficient * 0.05f / calcPs;// 160128
            // Log.d("sherlock", "minDBAvg::" + minDBAvg);
            waveSimpleAvg = waveSimpleAvg * 0.95f + minSnoreSimpleAvg * 0.05f;
            synchronized (showedList0) {
                showedList0.add(snoreDBTmpList.get(snoreDBTmpList.size() / 2));
                if (showedList0.size() > 50 && showedList0.size() > maxNum) {
                    showedList0.remove(0);
                }
            }
            synchronized (showedList01) {
                showedList01.add((float) dbAvg);
                if (showedList01.size() > 50 && showedList01.size() > maxNum) {
                    showedList01.remove(0);
                }
            }
            clacSnore151016(
                    snoreCurTimeTmpList.get(snoreCurTimeTmpList.size() / 2),
                    dbAvg);

        }
        // 把多余点去掉
        while (snoreDBTmpList.size() > samplePreSec) {
            snoreDBTmpList.remove(0);
            snoreCurTimeTmpList.remove(0);
        }

    }

    private void clacSnore151016(long curTime, float dbAv) {
        // 1.连续上升或者下降2.间隔时长，3.连续3个波峰
        if (dbAv > 0 && dbAv < 120) {// 防止负无穷数据影响
            // 累计波峰点
            // 累计鼾声的时间
            // 计算实时响度与均值的差值，根据差值的大小确定判断动作
            float compareTmp = dbAv - waveSimpleAvg;
            // Log.d("sherlock", "compareTmp::" + compareTmp);
            if (compareTmp > SnoreCoefficient) {
                compareTmpList.add(compareTmp);
                // lastDbavMaxTmp = Math.max(lastDbavMaxTmp, dbAv);

                if (waveCrestValue < dbAv) {
                    waveCrestValue = dbAv;
                    waveCrestTime = curTime;
                }
                if (compareTmpList.size() == 1) {
                    // 第一个符合条件点，记录波动开始时间
                    snoreStartTime = curTime;

                }

            } else {
                // 计算低点平均值
                compareTmpAvg = compareTmpAvg * 0.95f + dbAv * 0.05f;
//                Log.d("sherlock", "compareTmpAvg::" + compareTmpAvg);
                if (compareTmpAvg < 20) {
                    VSnoren = 25;
                    SnoreCoefficient = 9.0f;
                } else if (compareTmpAvg < 30) {
                    VSnoren = 35;
                    SnoreCoefficient = 7.0f;
                } else if (compareTmpAvg < 40) {
                    VSnoren = 45;
                    SnoreCoefficient = 5.0f;
                } else if (compareTmpAvg < 50) {
                    VSnoren = 50;
                    SnoreCoefficient = 3.0f;
                } else {
                    VSnoren = 55;
                    SnoreCoefficient = 1.0f;
                }
                if (lastCompareTmp > SnoreCoefficient) {

                    // 第一个不符合条件点，记录波动结束时间
                    snoreStopTime = curTime;

                    // long snoreTime = snoreStopTime - snoreStartTime;
                    long snoreTime = snoreStopTime - snoreStartTime;
                    // long snoreCycleTime = snoreStopTime -
                    // lastSnoreStopTime;// 波动周期时长
                    long snoreCycleTime = waveCrestTime - lastWaveCrestTime;// 波动周期时长
                    Log.e("sherlock", "波峰" + compareTmpList.size() + "个点::"
                            + "时长::" + snoreTime + "周期时长::" + snoreCycleTime);
                    // snoreTime < TSnoreMax && snoreTime > TSnoreMin&&
                    // 判断一个波动周期是否符合范围
                    if (snoreCycleTime > TSnoreRateMin && snoreCycleTime < TSnoreRateMax) {
                        // sendMsg2(22, 1);//振动
                        // 清零次数
                        if (snoreCountForTempTime.size() > 0 && snoreStopTime - snoreCountForTempTime.get(snoreCountForTempTime.size() - 1) > TSnoreRateMax) {
                            snoreCountForTemp = 0;
                            snoreCountForTempTime.clear();
                        }
                        snoreCountForTemp++;// 缓存鼾声次数+1
                        snoreCountForTempTime.add(snoreStopTime);
                        // Log.e("sherlock", "波形符合::" + snoreCountForTemp);

                        // 只要最近N个鼾声的，多出的就去掉
                        if (snoreCountForTempTime.size() > SnoreCountCon + 1) {
                            snoreCountForTempTime.remove(0);
                        }
                        // 判断N个波动周期是否符合范围
                        long snoreNTime = snoreStopTime - snoreCountForTempTime.get(0);
                        if (snoreCountForTemp >= SnoreCountCon) {
                            if (snoreNTime > (TSnoreRateMin) * SnoreCountCon && snoreNTime < (TSnoreRateMax) * SnoreCountCon) {
                                // Log.d("sherlock", "多个波形时长也符合::"
                                // + snoreCountForTemp);
                                startRecord();// 处理开始录音
                                if (waveCrestValue > VSnoren) {// 鼾声要满足条件，最高声音
                                    if (snoreCountForTemp == SnoreCountCon) {// 如果是第一次满足次数则多加几个
                                        snoreCountAtNight += SnoreCountCon;
                                        snoreAccountTime += curTime - snoreCountForTempTime.get(0);
                                    } else {
                                        snoreCountAtNight++;
                                        snoreAccountTime += snoreStopTime - snoreCountForTempTime.get(snoreCountForTempTime.size() - 2);
                                    }
                                    // 处理振动等级
                                    long durTime = curTime - lastVBTime;
                                    if (durTime > 600000) {//十分钟间隔
                                        vbLv = MySharedPreferencesUtil.getVbLv(context);
                                        if (System.currentTimeMillis() - startTime < Hour * 2) {
                                            if (vbLv - 2 > 0) {
                                                vbLv = vbLv - 2;
                                            } else {
                                                vbLv = 1;
                                            }
                                        }
                                        if (System.currentTimeMillis() - startTime > Hour * 2 && System.currentTimeMillis() - startTime < Hour * 4) {
                                            if (vbLv - 3 > 0) {
                                                vbLv = vbLv - 3;
                                            } else {
                                                vbLv = 1;
                                            }
                                        }
                                        if (System.currentTimeMillis() - startTime > Hour * 4 && System.currentTimeMillis() - startTime < Hour * 6) {
                                            if (vbLv - 4 > 0) {
                                                vbLv = vbLv - 4;
                                            } else {
                                                vbLv = 1;
                                            }
                                        }
                                        if (System.currentTimeMillis() - startTime > Hour * 6) {
                                            if (vbLv - 5 > 0) {
                                                vbLv = vbLv - 5;
                                            } else {
                                                vbLv = 1;
                                            }
                                        }

                                    }
                                    if (durTime > 25000) {
                                        Log.e("lalalala", "" + vbLv + "     " + (System.currentTimeMillis() - startTime));

                                        vbLvCount++;
                                        sendMsg2(CusConstant.SNORE_STOP, vbLv);// 发送振动通知
                                        sendMsg2(CusConstant.SNORE_STOP_COUNT, vbLvCount);// 发送止鼾次数
                                        vbLv++;
                                        if (vbLv > MySharedPreferencesUtil.getMaxVbLv(context)) {

                                            vbLv = MySharedPreferencesUtil.getMaxVbLv(context);
                                        }
                                        MySharedPreferencesUtil.setVbLv(context, vbLv);
                                        lastVBTime = curTime;
                                    } else {
                                    }

                                } else {
                                    Log.d("sherlock", "声音太小::" + waveCrestValue);
                                }
                                float breathRateTmp = 60000 * SnoreCountCon
                                        / snoreNTime;
                                breathRateTmp = Math.max(breathRateTmp, 12);
                                breathRateTmp = Math.min(breathRateTmp, 20);
                                breathRate = (float) (breathRate * 0.95 + breathRateTmp * 0.05);
                                Log.d("sherlock", "breathRateTmp::" + breathRate);

                                sendMsg2(CusConstant.SNORE_COUNT, snoreCountAtNight); // 发送鼾声次数
                                Log.d("snoreAccountTime", "snoreAccountTime::" + snoreAccountTime + "    " + (snoreAccountTime / 1000));
                                sendMsg2(CusConstant.SNORE_TIME, (snoreAccountTime / 1000));// 发送鼾声时长

                                sendMsg2(CusConstant.BREATH_RATE, Math.round(breathRate));// 最近几次的平均呼吸频率

                                lastSnoreStopTime = curTime;
                            } else {
                                Log.e("sherlock", "N个鼾声时长不符合::" + snoreNTime);
                            }
                        } else {
                            Log.d("sherlock", "波形次数不够::" + snoreCountForTemp);
                        }
                    } else {
                        // 清零鼾声缓存数据
                        if (isInserSnore) {
                            Log.e("sherlock", "clacSnore:   鼾声停止了  " + snoreStopTime);
                            isInserSnore = false;
                        }

                        snoreCountForTemp = 0;
                        snoreCountForTempTime.clear();
                        // 计算动作
                        if (curTime - lastActTime > 5000) {
                            actCountFor2min++;
                            snore2AllActCount++;
                            snore5AllMoveCount++;
                            sendMsg2(CusConstant.ACT_COUNT, snore2AllActCount);
                            // sendMsg2(22, vbLv);// 发送振动通知
                        }
                        // 计算翻身
                        if (snoreTime > TTureMax) {
                            if (curTime - snoreLastActTureTime > 10000) {
                                snore10AllTurnCount++;
                                snore2AllTurnCount++;
                                sendMsg2(CusConstant.TRUN_COUNT, snore2AllTurnCount);
                                Log.e("xx", " 翻身  发送  震动关闭。。。。。。");
                                sendMsg2(21, null);//震动关闭
                            }
                            snoreLastActTureTime = curTime;
                        }
                        lastActTime = curTime;
                    }

                    lastSnoreStartTime = snoreStartTime;
                    lastSnoreStopTime = snoreStopTime;
                    lastWaveCrestTime = waveCrestTime;
                    lastWaveCrestValue = waveCrestValue;

                } else {


                }
                if (curTime - lastSnoreStopTime > 60000) {// 间隔时长过长，停止录音
                    stopRecord();
                }
                calcMove(curTime);
                // trunCountBySleepDur(curTime);
                // 遇到不符合的点，则清空LIST
                compareTmpList.clear();
                // lastDbavMaxTmp = 0;
                waveCrestTime = 0;
                waveCrestValue = 0;

            }

            lastCompareTmp = compareTmp;
            compareTmp = Math.round(compareTmp * 10) / 10.00f;

            showedList1
                    .add((float) ((compareTmp > SnoreCoefficient) ? compareTmp
                            : 0));
            if (showedList1.size() > 50 && showedList1.size() > maxNum) {
                showedList1.remove(0);
            }

        } else {
            Log.d("sherlock", "声音异常");
        }
    }

    // private void sendMsg(int w, Object v) {
    // Message msg = new Message();
    // msg.what = w;
    // msg.obj = v;// 响度平滑线
    // // msg.obj = compareTmp > 10 ? 10 : compareTmp;
    // handler.sendMessage(msg);
    // }

    private void sendMsg2(int w, Object v) {
        Message msg = new Message();
        msg.what = w;
        msg.obj = v;// 响度平滑线
        // msg.obj = compareTmp > 10 ? 10 : compareTmp;
        handler.sendMessage(msg);
    }

    private void startRecord() {
        try {

            recorder.startWirteMP3(buffer,sleepID);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 处理体动的曲线 5分钟记录一次
     */
    private void calcMove(long curTime) {
        if (curTime - moveStartTime >= moveTime) {
            moveStartTime = curTime;

            SimpleDateFormat dateFormat = new SimpleDateFormat(
                    "yyyy-MM-dd HH:mm:ss:SSS");
            String timeName = "";
            timeName = dateFormat.format(curTime);
            HMInfo hi = new HMInfo();
            hi.settTime(timeName);
            hi.settValue(snore5AllMoveCount);
            allMove.add(hi);
            Log.e("lg", "体动次数::" + allMove.size());

            showedList11.add((float) snore5AllMoveCount);
            if (showedList11.size() > 50 && showedList11.size() > maxNum) {
                showedList11.remove(0);
            }

            snore5AllMoveCount = 0;
        }
    }

    private void trunCountBySleepDur(long curTime) {

    }

    public void monitorStop() {
        if (isLoop) {
            isLoop = false;
        }
    }

    public void monitorDestory() {
        monitorStop();
        if (recorder != null) {
            recorder.clean();
            recorder = null;
        }
        buffer = null;
    }

    /**
     * 向上取最接近iint的2的幂次数.比如iint=320时,返回256
     *
     * @param iint
     * @return
     */
    private int up2int(int iint) {
        int ret = 1;
        while (ret <= iint) {
            ret = ret << 1;
        }
        return ret >> 1;
    }

    // 快速傅里叶变换
    public void fft(Complex[] xin, int N) {
        int f, m, N2, nm, i, k, j, L;// L:运算级数
        float p;
        int e2, le, B, ip;
        Complex w = new Complex();
        Complex t = new Complex();
        N2 = N / 2;// 每一级中蝶形的个数,同时也代表m位二进制数最高位的十进制权值
        f = N;// f是为了求流程的级数而设立的
        for (m = 1; (f = f / 2) != 1; m++)
            ; // 得到流程图的共几级
        nm = N - 2;
        j = N2;
        /****** 倒序运算——雷德算法 ******/
        for (i = 1; i <= nm; i++) {
            if (i < j)// 防止重复交换
            {
                t = xin[j];
                xin[j] = xin[i];
                xin[i] = t;
            }
            k = N2;
            while (j >= k) {
                j = j - k;
                k = k / 2;
            }
            j = j + k;
        }
        /****** 蝶形图计算部分 ******/
        for (L = 1; L <= m; L++) // 从第1级到第m级
        {
            e2 = (int) Math.pow(2, L);
            // e2=(int)2.pow(L);
            le = e2 + 1;
            B = e2 / 2;
            for (j = 0; j < B; j++) // j从0到2^(L-1)-1
            {
                p = 2 * ((float) 3.1415926) / e2;
                w.real = Math.cos(p * j);
                // w.real=Math.cos((double)p*j); //系数W
                w.image = Math.sin(p * j) * -1;
                // w.imag = -sin(p*j);
                for (i = j; i < N; i = i + e2) // 计算具有相同系数的数据
                {
                    ip = i + B; // 对应蝶形的数据间隔为2^(L-1)
                    t = xin[ip].cc(w);
                    xin[ip] = xin[i].cut(t);
                    xin[i] = xin[i].sum(t);
                }
            }
        }
    }

    /**
     * 创建文件
     *
     * @author omesoft_lgc
     * @date 2015-9-29 上午11:38:22
     */
    class CreateTextTask extends AsyncTask<Void, Integer, Integer> {
        private Context context;

        CreateTextTask(Context context) {
            this.context = context;
        }

        /**
         * 运行在UI线程中，在调用doInBackground()之前执行
         */
        @Override
        protected void onPreExecute() {

        }

        /**
         * 后台运行的方法，可以运行非UI线程，可以执行耗时的方法
         */
        @Override
        protected Integer doInBackground(Void... params) {
            fileName = CreateFiles.CreateText(sleepID);
            Log.e("test", "创建的文件名.." + fileName);
            return 0;
        }

        /**
         * 运行在ui线程中，在doInBackground()执行完毕后执行
         */
        @Override
        protected void onPostExecute(Integer integer) {
        }

        /**
         * 在publishProgress()被调用以后执行，publishProgress()用于更新进度
         */
        @Override
        protected void onProgressUpdate(Integer... values) {

        }
    }

    public int getSleepTime() {
        return sleepTime;
    }

    public void setSleepTime(int sleepTime) {

        this.sleepTime += sleepTime;
        if (this.sleepTime > 1000)
            this.sleepTime = 200;
    }

    public int getMaxNum() {
        return maxNum;
    }

    public void setMaxNum(int maxNum) {
        this.maxNum = maxNum;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }


}
